import backtrader as bt
import pandas

from back_test.strategies.RVI_strategy import RVI_stg

# from strategies.ATR import AverageTrueRange

# Instantiate Cerebro engine
cerebro = bt.Cerebro()

# # Set data parameters and add to Cerebro
df = pandas.read_csv('../csv/TSLA.csv',
                            parse_dates=True,
                            index_col=0
                            )
# choose date from 2018-01-01 to 2020-12-25
df = df.loc['2023-01-01':'2024-01-01']
print(df.head())
# Pass it to the backtrader datafeed and add it to the cerebro
data = bt.feeds.PandasData(dataname=df)

# data = bt.feeds.YahooFinanceCSVData(
#     dataname='../csv/AAPL.csv',
#     fromdate=datetime.datetime(2018, 1, 1),
#     todate=datetime.datetime(2020, 12, 25),
# )
cerebro.adddata(data)

# Add strategy to Cerebro
cerebro.addstrategy(RVI_stg)
cerebro.broker.setcash(12000.0)
# Default position size
cerebro.addsizer(bt.sizers.SizerFix, stake=3) # explain: https://www.backtrader.com/docu/sizers/sizers/
# cerebro.addsizer(bt.sizers.PercentSizer,percents=99)
if __name__ == '__main__':
    # Run Cerebro Engine
    start_portfolio_value = cerebro.broker.getvalue()

    cerebro.run()

    end_portfolio_value = cerebro.broker.getvalue()
    pnl = end_portfolio_value - start_portfolio_value
    print(f'Starting Portfolio Value: {start_portfolio_value:2f}')
    print(f'Final Portfolio Value: {end_portfolio_value:2f}')
    print(f'PnL: {pnl:.2f}')